package dao

import (
	"1-12zk/inits"
	"1-12zk/models"
)

func AddRoom(userId int) models.Room {
	room := models.Room{
		RoomUserId: userId,
	}
	err := inits.DB.Debug().Create(&room).Error
	if err != nil {
		return models.Room{}
	}
	user := models.RoomUser{
		RoomId: int(room.ID),
		UserId: userId,
		Type:   1,
	}
	err = inits.DB.Debug().Create(&user).Error
	if err != nil {
		return models.Room{}
	}
	return room
}

func CreateRoomUser(roomId, UserId int) bool {
	user := models.RoomUser{
		RoomId: roomId,
		UserId: UserId,
		Type:   3,
	}
	err := inits.DB.Debug().Create(&user).Error
	if err != nil {
		return false
	}
	return true
}

func UserFind(id uint) bool {
	var room models.User
	err := inits.DB.Debug().Where("id = ?", id).Find(&room).Error
	if err != nil {
		return false
	}
	return true
}

func Del(RoomId models.Room) error {
	return inits.DB.Debug().Delete(RoomId).Error
}

func Update(user models.RoomUser) error {
	return inits.DB.Debug().Where("room_user_id = ?", user.RoomUserId).Updates(user.Type).Error
}
